package com.rf.richfitwheel.workflow.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rf.richfitwheel.admin.sys.vo.UserVO;
import com.rf.richfitwheel.common.contants.Constant;
import com.rf.richfitwheel.common.utils.PageUtils;
import com.rf.richfitwheel.common.utils.StringUtils;
import com.rf.richfitwheel.common.utils.UuidUtil;
import com.rf.richfitwheel.workflow.dao.WfAuthRuleMapper;
import com.rf.richfitwheel.workflow.dao.WfStationProcessOrderMapper;
import com.rf.richfitwheel.workflow.model.*;
import com.rf.richfitwheel.workflow.service.WfProcessPageService;
import com.rf.richfitwheel.workflow.service.WfStationProcessOrderService;
import net.sf.json.JSONArray;
import net.sf.json.JSONNull;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Service("wfStationProcessOrderService")
public class WfStationProcessOrderServiceImpl implements WfStationProcessOrderService {
    @Autowired
    private WfStationProcessOrderMapper wfStationProcessOrderMapper;
    @Autowired
    private WfProcessPageService wfProcessPageService;

    @Autowired
    private WfAuthRuleMapper wfAuthRuleMapper;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        PageUtils pageUtils = PageUtils.getPageInfo(params);
        Page<Map<String, Object>> p = PageHelper.startPage(pageUtils.getPage(), pageUtils.getLimit());
        WfStationProcessOrder wfProcessFolder = (WfStationProcessOrder) JSONObject.toBean(JSONObject.fromObject(params.get("dataForm")), WfStationProcessOrder.class);
        WfStationProcessOrderExample example = new WfStationProcessOrderExample();
        WfStationProcessOrderExample.Criteria criteria = example.createCriteria();
        List<WfStationProcessOrder> list = wfStationProcessOrderMapper.selectByExample(example);
        pageUtils = new PageUtils(p);
        return pageUtils;
    }

    @Override
    public List<WfStationProcessOrder> selectListByStationProcessId(String stationProcessId) {
        WfStationProcessOrderExample example = new WfStationProcessOrderExample();
        WfStationProcessOrderExample.Criteria criteria = example.createCriteria();
        criteria.andStationProcessIdEqualTo(stationProcessId);
        example.setOrderByClause(" approve_order ");
        List<WfStationProcessOrder> wfStationProcessOrders = wfStationProcessOrderMapper.selectByExample(example);
        return wfStationProcessOrders;
    }

    @Override
    public void deleteByStationProcessId(String stationProcessId) {
        WfStationProcessOrderExample example = new WfStationProcessOrderExample();
        WfStationProcessOrderExample.Criteria criteria = example.createCriteria();
        criteria.andStationProcessIdEqualTo(stationProcessId);
        wfStationProcessOrderMapper.deleteByExample(example);
    }

    @Override
    public void addWfStationProcessOrder(WfStationProcessOrder wfStationProcessOrder, UserVO userVO) {
        Date nowDate = new Date();
        wfStationProcessOrder.setId(StringUtils.isBlank(wfStationProcessOrder.getId()) ? UuidUtil.get32UUID() : wfStationProcessOrder.getId());
        wfStationProcessOrder.setCreater(userVO.getId());
        wfStationProcessOrder.setCreateTime(nowDate);
        wfStationProcessOrder.setModTime(nowDate);
        wfStationProcessOrder.setModUser(userVO.getId());
        wfStationProcessOrderMapper.insertSelective(wfStationProcessOrder);
    }

    @Override
    public List<WfStationProcessOrder> saveWfStationProcessOrderAndReturnList(JSONArray stationDataList, WfStationProcess wfStationProcess, UserVO userVO) {
        List<WfStationProcessOrder> wfStationProcessOrders = new ArrayList<>();
        this.deleteByStationProcessId(wfStationProcess.getId());//先清空
        for (int i = 0; i < stationDataList.size(); i++) {
            JSONObject stationData = stationDataList.getJSONObject(i);
            WfStationProcessOrder wfStationProcessOrder = new WfStationProcessOrder();
            wfStationProcessOrder.setId(StringUtils.isNotBlank(stationData.get("id")) ? stationData.getString("id") : null);
            wfStationProcessOrder.setStationProcessId(wfStationProcess.getId());
            wfStationProcessOrder.setOrgCode(stationData.getString("orgCode"));
            wfStationProcessOrder.setOrgName(stationData.getString("orgName"));
            wfStationProcessOrder.setDeptCode(stationData.getString("deptCode"));
            wfStationProcessOrder.setDeptName(stationData.getString("deptName"));
            wfStationProcessOrder.setModuleType(stationData.getString("moduleType"));
            wfStationProcessOrder.setStationId(stationData.getString("stationId"));
            wfStationProcessOrder.setStationName(stationData.getString("stationName"));
            wfStationProcessOrder.setTaskName(stationData.getString("taskName"));
            wfStationProcessOrder.setAuthType((stationData.containsKey("authType") && stationData.get("authType") != null && !(stationData.get("authType") instanceof JSONNull)) ? stationData.getString("authType") : null);
            wfStationProcessOrder.setSourceFrom((stationData.containsKey("sourceFrom") && stationData.get("sourceFrom") != null && !(stationData.get("sourceFrom") instanceof JSONNull)) ? stationData.getString("sourceFrom") : null);
            wfStationProcessOrder.setLastStation((stationData.containsKey("lastStation") && stationData.get("lastStation") != null && !(stationData.get("lastStation") instanceof JSONNull)) ? stationData.getString("lastStation") : Constant.COMMON_YES_OR_NO_NO);
            wfStationProcessOrder.setNeedKey((stationData.containsKey("needKey") && stationData.get("needKey") != null && !(stationData.get("needKey") instanceof JSONNull)) ? stationData.getString("needKey") : Constant.COMMON_YES_OR_NO_NO);
            WfProcessPage wfProcessPage = wfProcessPageService.getByPageCode(stationData.getString("pageCode"));
            //WfProcessPage wpp = wfProcessPageService.getByFolderCodeAndPageType(wfStationProcess.getFolderCode(), wfProcessPage.getPageType());
            if (!ObjectUtils.isEmpty(wfProcessPage)) {
                wfStationProcessOrder.setPageCode(wfProcessPage.getPageCode());
            }
            wfStationProcessOrder.setApproveOrder(i + 1);
            this.addWfStationProcessOrder(wfStationProcessOrder, userVO);
            wfStationProcessOrders.add(wfStationProcessOrder);
            //如果有授权规则的话
            JSONArray jsa = stationData.containsKey("authRules") ? stationData.getJSONArray("authRules") : null;
//            wfAuthRuleService.save("", wfStationProcessOrder.getStationId(), wfStationProcessOrder.getAuthType(), jsa);
            if(jsa != null){
                WfAuthRuleExample ruleExample = new WfAuthRuleExample();
                WfAuthRuleExample.Criteria ruCriteria = ruleExample.createCriteria();
                ruCriteria.andStationProcessOrderIdEqualTo(wfStationProcessOrder.getId());
                wfAuthRuleMapper.deleteByExample(ruleExample);
                for(Object obj : jsa){
                    JSONObject jsonObject = JSONObject.fromObject(obj);
                    WfAuthRule wfAuthRule = (WfAuthRule) JSONObject.toBean(jsonObject, WfAuthRule.class);
                    wfAuthRule.setId(UuidUtil.get32UUID());
                    /*wfAuthRule.setRuleType(jsonObject.containsKey("ruleType") ? jsonObject.getString("ruleType") : null);
                    wfAuthRule.setRuleField(jsonObject.containsKey("ruleField") ? jsonObject.getString("ruleField") : null);
                    wfAuthRule.setRuleFieldType(jsonObject.containsKey("ruleFieldType") ? jsonObject.getString("ruleFieldType") : null);
                    wfAuthRule.setRuleCondition(jsonObject.containsKey("ruleCondition") ? jsonObject.getString("ruleCondition") : null);
                    wfAuthRule.setRuleCur(jsonObject.containsKey("ruleCur") ? jsonObject.getString("ruleCur") : null);
                    wfAuthRule.setRuleAmt(jsonObject.containsKey("ruleAmt") ? new BigDecimal(jsonObject.getString("ruleAmt")) : null);
                    wfAuthRule.setToStationId(jsonObject.containsKey("toStationId") ? jsonObject.getString("toStationId") : null);*/
                    wfAuthRule.setStationProcessOrderId(wfStationProcessOrder.getId());
                    wfAuthRuleMapper.insert(wfAuthRule);
                }
            }
        }
        return wfStationProcessOrders;
    }

    @Override
    public WfStationProcessOrder selectById(String id) {
        return wfStationProcessOrderMapper.selectByPrimaryKey(id);
    }

    @Override
    public void updateAuthType(String stationProcessOrderId, String authType) {
        WfStationProcessOrder wfStationProcessOrder = new WfStationProcessOrder();
        wfStationProcessOrder.setId(stationProcessOrderId);
        wfStationProcessOrder.setAuthType(authType);
        wfStationProcessOrderMapper.updateByPrimaryKeySelective(wfStationProcessOrder);
    }
}